//
//  DataListView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/7.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//  需要根据内容动态调整frame

import UIKit

class DataListView: BaseView,UITableViewDelegate,UITableViewDataSource {
    
    var table : RefreshedTableView?
    var label5 : UILabel?
    var label6 : UILabel?
    var source : Array<TableCellModel> = [] {
        didSet{
            table!.reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configTable(delegate: nil)
    }
    
    init(frame: CGRect,delegate : beginRefresh) {
        super.init(frame: frame)
        configTable(delegate: delegate)
    }
    
    func handleData(model: TurnOverModel) {
        label5!.text = NumberToString.intToFloatString(number: model.sumPrice, radix: 100)
        label6!.text = NumberToString.intToFloatString(number: model.sumCommision, radix: 100)
        source = model.dataSrc
        table?.smileView.endRefresh()
    }
    
    func appendData(model : TurnOverModel) -> Void {
        //增加数据
        source.append(contentsOf: model.dataSrc)
        label5!.text = String(model.sumPrice)
        label6!.text = String(model.sumCommision)
        table?.reloadData()
//        table?.startAnimation = false
    }
    
    func startAppend() -> Void {
//        table?.startAnimation = true
    }
    
    private func configTable(delegate : beginRefresh?) -> Void {
        table = RefreshedTableView.init(frame: self.bounds, style: .plain)
        table!.delegate = self
        table!.dataSource = self
        table?.allowsSelection = false
        table!.separatorStyle = .none
        table?.refreshDelegate = delegate
        table!.register(UINib.init(nibName: "DataListCell", bundle: Bundle.main), forCellReuseIdentifier: "Data")
        self.addSubview(table!)
        let head = UIView.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: self.width(), height: 88 * YXHeightScale)))
        head.backgroundColor = UIColor.colorWithHexString(hex: "efeff4")
        //手机类型标签
        let label = configHeadLabel(title: "手机类型", idx: 0,Bold: false)
        head.addSubview(label)
        
        let label1 = configHeadLabel(title: "结算金额", idx: 1,Bold: false)
        head.addSubview(label1)
        
        let label2 = configHeadLabel(title: "提成", idx: 2,Bold: false)
        head.addSubview(label2)
        
        let label3 = configHeadLabel(title: "成交时间", idx: 3,Bold : false)
        head.addSubview(label3)
        table!.tableHeaderView = head
        
        let footer = UIView.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: self.bounds.size.width, height: 88 * YXHeightScale)))
        footer.backgroundColor = UIColor.colorWithHexString(hex: "ededed")
        let label4 = configHeadLabel(title: "统计金额", idx: 0, Bold: true)
        footer.addSubview(label4)
        label5 = configHeadLabel(title: "0.00", idx: 1, Bold: true)
        footer.addSubview(label5!)
        label6 = configHeadLabel(title: "0.00", idx: 2, Bold: true)
        footer.addSubview(label6!)
        table!.tableFooterView = footer
    }
    
    private func configHeadLabel(title : String,idx : Int,Bold : Bool)->UILabel{
        let label = UILabel.init()
        label.textColor = UIColor.colorWithHexString(hex: "2c2c2c")
        if Bold == true {
            label.font = UIFont.boldSystemFont(ofSize: 26 * YXWidthScale)
        }else{
            label.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        }
        label.textAlignment = .center
        label.frame = CGRect.init(origin: CGPoint.init(x: CGFloat(idx) * self.bounds.size.width / 4, y: 0), size: CGSize.init(width: self.bounds.size.width / 4, height: 88 * YXHeightScale))
        label.text = title
        return label
    }
    
    //代理方法 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return source.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 88 * YXHeightScale
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "Data")!
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let cel = cell as! DataListCell
        let mod = source[indexPath.row]
        if indexPath.row % 2 == 0 {
            mod.colorFlag = true
        }
        cel.model = source[indexPath.row]
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offY = scrollView.contentOffset.y
        if offY <= 0 {
            table!.smileView.scrollDidScroll()
        }
    }
    
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        table!.smileView.scrollDidEndDraging()
        let queue = DispatchQueue.init(label: "Global")
        queue.async {
            if self.table!.contentSize.height <= self.table!.frame.size.height {
                if self.table!.contentOffset.y > 20 {
                    self.table!.refreshDelegate.loadNext!()
                }
            }else{
                if self.table!.contentOffset.y > (self.table!.contentSize.height-self.table!.frame.size.height+20) {
                    //加载下一页
                    self.table!.refreshDelegate.loadNext!()
                }
            }
        }
    }

}
